package com.aysafety.core.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Common {

    private final static Logger logger = LoggerFactory.getLogger(Common.class);

    public static String getRowKey(List<String> params, String timestamp) {
        String rowKey = "";
        for (String param : params) {
            rowKey += param;
        }
        rowKey = timestamp + ".:." + MD5.md5ToLower(rowKey);
        return rowKey;
    }

    public static Map<String, Object> getData(List<Map<String, Object>> list, int size, String startRow,
        String firstStartRow) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("list", new ArrayList<Map<String, Object>>());
        result.put("firstStartRow", null);
        result.put("lastStartRow", null);
        int maxNum = list.size();
        if (maxNum > 1 && StringUtils.isNotBlank(startRow)) {
            Map<String, Object> temp = list.get(list.size() - 1);
            String lastStartRow = temp.get("rowkey").toString();
            if (StringUtils.isNotBlank(startRow)) {
                list.remove(0);
            } else {
                list.remove(maxNum - 1);
            }
            if (maxNum > size) {
                result.put("lastStartRow", lastStartRow);
            }
            if (StringUtils.isNotBlank(startRow) && StringUtils.isNotBlank(firstStartRow)) {
                result.put("firstStartRow", firstStartRow);
            }
            result.put("list", list);
        } else {
            result.put("list", list);
        }
        return result;
    }

}
